<?php

include_once "ini.php";

//repaertoire de stockage d'image
$repertoireDestination = "images/";

//nom réel du fichier
$nomDestination = $_FILES['fichier']['name'];

//nom temporaire du fichier (sur le serveur)
$tmp = $_FILES['fichier']['tmp_name'];
$droit = $_POST['droit'];
//$id_user=$_SESSION['id_user'];

//permet de vérifier si la fichier a été upload via http
if (is_uploaded_file($tmp))
{
    //on déplace le fichier dans le répertoire final

    if (move_uploaded_file($tmp, $repertoireDestination.$nomDestination))
    {
        //header('Location: test.html');

        dbconnect();

        $sql = "INSERT INTO `citu_dev`.`images` (`id_image` , `id_user` , `nom_image` , `coord_image` , `droit` ) VALUES ( NULL , '1', '".$nomDestination."', '', '".$droit."' );";
        $sqlresults = mysql_query($sql);
        if (!$sqlresults)
        {
            echo '{"status":"ERROR: '.$sql.'"}';
        }

        dbclose();
        list ($width, $height) = getimagesize($repertoireDestination.$nomDestination);
        $file = "xml_test.xml";
        $chaine = "<?xml version='1.0' encoding='utf-8'?>
<images>
    <image nom=\"http://dev.citu.fr/infinity/images/".$nomDestination."\" largeur=\"".$width."\" hauteur=\"".$height."\" />
</images>";
//a prévoir ds le xml :url et nom sans extension, droit d'accès
$fp = fopen($file, "w");
fputs($fp, $chaine);
fclose($fp);

}
else
{ 
echo "Erreur de transfert 1er 
<br/>
<a href='test.html'>Retour</a>";
}
}
else
{ 
echo "Erreur de transfert 
<br/>
<a href='test.html'>Retour</a>";
}
$percent = 0.5;
$repertoirePreview = "imagespreview/";
// Calcul des nouvelles dimensions
list($width, $height) = getimagesize($repertoireDestination.$nomDestination);
$new_width = $width*$percent;
$new_height = $height*$percent;
// Redimensionnement
$image_new = @imagecreatetruecolor($new_width, $new_height)
or die ("Cannot Initialize new GD image stream");
$blanc = ImageColorAllocate($image_new, 255, 255, 255);
$extention = explode('.', $nomDestination);
$extention = strtolower($extention[count($extention)-1]);
if ($extention == 'jpeg' || $extention == 'jpg')
{
$image = imagecreatefromjpeg($repertoireDestination.$nomDestination);
imagecopyresampled($image_new, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// enregistrement
imagejpeg($image_new, $repertoirePreview.$nomDestination);
}
else if ($extention == 'png')
{
$image = imagecreatefrompng($repertoireDestination.$nomDestination);
imagecopyresampled($image_new, $image, 0, 0, 0, 0, $new_width, $new_height, $newsize[0], $newsize[1]);
imagepng($image_new, $repertoirePreview.$nomDestination);
}
elseif ($extention == 'gif')
{
$image = imagecreatefromgif($repertoireDestination.$nomDestination);
imagecopyresampled($image_new, $image, 0, 0, 0, 0, $new_width, $new_height, $newsize[0], $newsize[1]);
imagegif($image_new, $repertoirePreview.$nomDestination);
}
else
{
echo "Format incorrect.";
}
imagedestroy($image_new);
$url="http://dev.citu.fr/infinity/images/".$nomDestination;
//echo "<script type='text/javascript'> window.opener.uploadSucceed('".$url."'); </script>";
//echo "<script type='text/javascript'> self.close('lepopup'); </script>";
echo "<script type='text/javascript'> uploadSucceed('".$url."'); </script>";
?>
